DEV Community

Shahriyar Al Mustakim Mitul
Shahriyar Al Mustakim Mitul

Posted on • Updated on

Docker series (Part 2): Starting an nginx Web server on Ubuntu

Let's type this in our terminal to run a nginx web server

sudo docker container run --publish 80:80 nginx
Enter fullscreen mode Exit fullscreen mode

Image description

go to your browser and type "localhost". You can see a page of nginx.

This is what basically happened

Image description

Stop the container using Ctrl+C

Now, it is not active any more.

Let's create another one and this time , we will run it in the back ground.

sudo docker container run --publish 80:80 --detach nginx
Enter fullscreen mode Exit fullscreen mode

This detach command actually runs it in the background and after we run it, we get a container ID

Image description

again, you can go to your browser and type "localhost" and see the nginx server.

now go to the terminal and type

sudo docker container ls
Enter fullscreen mode Exit fullscreen mode

Image description
This shows which container is running.

Let's stop it

sudo docker container stop <1st few digits of the container id>
Enter fullscreen mode Exit fullscreen mode

Image description
So, it has been stopped.

Now, let's see how many containers we actually created because we stopped them but not deleted them ever.

sudo docker container ls -a
Enter fullscreen mode Exit fullscreen mode

Image description
You can also go to your VS Code studio and install "Docker" from extensions . This will also track how many containers etc are there

Image description

also, you can see some unique name here like "elastic_saha"", "elastic_newton". This was randomly provided as you did not provide them.
Let's provide them a name and create another container .

sudo docker container run --publish 80:80 --detach --name mitul_shahriyar nginx
Enter fullscreen mode Exit fullscreen mode

Image description
Your VS code will look like this

Image description

You can go to your browser & type for "localhost" to see a nginx server.

let's see all the container list again

sudo docker container ls -a
Enter fullscreen mode Exit fullscreen mode

Image description
let's see some logs of this container which we gave the name "mitul_shahriyar"

Image description

Also , you may know commands to run docker or play with
Image description

Now, lets remove them

sudo docker container rm <1st few digits of container id>
Enter fullscreen mode Exit fullscreen mode

Image description

We have 1 container running which we will forcefully remove

Image description

Top comments (1)

Collapse
 
moopet profile image
Ben Sinclair

I imagine the only commands that need sudo are those where you're binding to a port below 1024. All the other ones should be run as a regular user.