[Docker] Starting a container on docker

Avocado Aun
7 min readMar 24, 2020

--

Part 2/5

In this post, we will learn about docker command structure and some of the commonly used basic docker commands and syntax. You should only run the command in the quotes. After this post, you should be able to:

check docker version

use --help to learn docker commands

start a nginx web server container

The docker command structure

To use docker cli, you can use cmd (Windows) or terminal (Mac). Type ‘docker’ to run any docker command. Docker command notation is:

docker <command><sub-command>(options)

To find out what commands that are supported by docker, type

docker --help

You will find two types of commands: (1) management command and (2) commands. Using file system analogy, management command is the like the main folder, and under it there smaller nested commands). For example, if we are to run any network related commands, then we will use the ‘network’ keyword; if we are to run a container then we use the ‘container’ keyword; followed by their respective options. Figure 1 shows all of the ‘main’ command in docker.

Type docker --help to see the commands supported in docker

For example, to run a container we run ‘docker container run imagename’. Meanwhile, to create a new network we run ‘docker image networkname

Starting a nginx web server on container (Part A)

In this section, we will start a nginx web server listening on port 80. Nginx is a web server which can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. Note that one of the power of docker is that we can run any ‘mini apps/micro services’ as long as they are discoverable (on docker hub). This allows us to choose the right services we want to deploy for our apps (similar to calling APIs). By combining these micro-services we can easily build a bigger apps without having to code from scratch (think lego building).These services are hosted on docker hub; each of them have a link (see image). Docker call these services as ‘images’.

Search on hub.docker.com to find the right image and use this command to pull image to localhost

You can browse on hub.docker.com to find the right image for your DevOps needs. If you want to pre-load these images, simply copy and paste the link to your cmd. Watch the clip below on how to preload a nginx image.

Few things to note here:

(1) if the image is already in the library, docker will skip the download.

(2) if you want a custom version of the image, you need to specify it like ‘docker pull nginx/latest’ or ‘docker pull nginx/1.16.1’

(3) if you need a custom image (not official but made by some contributors) then you need to specify their username/imagename like this: ‘docker pull avocado89/nginx/’

To check the images you currently stored in your machine, type:

docker image ls

For clean up, or to clear all the images type:

docker image prune then choose y

If the image is used by running container, you need to force remove them:

docker image prune -f

to remove all of the images, type:

docker image prune -a

This is the manual way to preload images if you think you will be developing offline for a while but already identified the images you need. Otherwise, when we run a container that requires an image; docker will automatically pull it from docker hub.

Starting a nginx web server on container (Part B)

So to recap, first we pull image from docker hub; then we run these images as containers. Note that you can run multiple containers from the same image. For example, webserver_1 from nginx, webserver_2 from nginx etc. We’ll be using nginx as our case study, since it doesn’t require many arguments to run and it should be easier for beginners to grasp. To learn more about how to start containers from other images, you can read their documentations from docker hub.

To start nginx, we type:

docker container run -p 80:80 nginx

The first parameter is a command from the management commands like {container, network, image…}. To start a container, we use commands under the container umbrella. This is followed by ‘run’, or you can use ‘start’ or ‘exec’ (more on this later). The -p indicates publish this container. Then we specify the ‘listeningport:hostport’ like ‘80:80’. The last parameter is the image name; here we use ‘nginx’. In summary, this line start a (web service) container using nginx image that is listening on port 80. Now, we can go checkout our new website by typing the following url on your browser:

localhost:80

#or 127.0.0.1:80

Hooray! You should see a webpage as shown above. Note that if you already have other services running on port 80, you need to change the container port to any random number when you specify the port mapping (-p) in the command, like from ‘80:80’ to ‘8080:80’ or ‘1234:80’

To list all the containers that are running, type:

docker image ls

# or type docker ps

The highlighted line shows our nginx web server’s container details

This will show us the containers we have started (or forgotten). If you zoom in on the image above, you’ll find the nginx web server we’ve started 3 minutes ago. It is running on the vanilla ‘nginx’ image, and it has a weird name of ‘friendly_chandrasakhar’. You will get other names depending on your machine; that’s because docker assign a random name to your container if you don’t name it. Now we should name our container, because it is always easier to work with a name like ‘avomax’ than some random names. But first i will stop this nginx web server and start a new container with the name ‘avoWEB’

To stop a running container, type:

docker container stop <containerID>

example: ‘docker container stop 53a0’

Here, we type ‘docker container stop 53a0’ to stop the nginx server. Note that I can either specify the container names or container ID. I use the ID in this case because that random dude name is too long. When you use ID, you only need to type the first few characters that are unique enough to identify the container. Check if the container has been stopped using ‘docker ps’ again.

To name the container, type:

docker container run --name <containerName> <imageName>

example: docker container run -p 80:80 --name avoWEB nginx

Now that we want to name our nginx server, we should keep all the parameters needed to start nginx while adding the --name parameter. This should run the nginx server with the name avoWEB. Use ‘docker ps’ to find out.

After naming your container with — name, type ‘docker ps’ to check the container name

So remember that containers are just like your babies, you should always name them to know who is your favourite child. In production, it is a life-saver to name containers so you know whether this id correspond to a database server, a web server or anything else.

Next, we want to try running the nginx container in the background. Most of the time, once the container is started, we ‘lose control’ of the prompt (cmd). Now, we can either start another terminal/cmd to continue our work, or we can run the container using the detach option (run in background). To do so, we use the -d option. For example, now we’ll stop avoWEB and restart it to run in background.

docker container stop avoWEB

docker container rm avoWEB

docker container run -p 80:80 --name avoWEB -d nginx

docker ps

Normally, we’ll have to check on our containers to check their health or for troubleshooting. Checking is as easy as using inspect or stats command.

To check the status of your container, type:

docker container inspect avoWEB

To check the details of the container, type:

docker container stats avoWEB

Check out the video below to learn how to start a ‘named’ container and checking on its run time status.

Yipee! If you are still here, congratulations on successfully hosting your web server using a container. Of course, you can add in your own web pages to nginx and expose your public IP if you want people to browse your websites. I found a great guide to show you how to do it here (optional).

Next, we will find out what’s inside a container, and how to ‘get into’ the container to do our stuffs. Part 3 here.

--

--

Avocado Aun
Avocado Aun

Written by Avocado Aun

I’m just a little boy, lost in the tech world. But remember, love is a riddle, and life with tech is more amazing than ever

No responses yet