How to run a basic Docker container

What is a Docker container ?

A container is a standardized software package that bundles code and dependencies for consistent and reliable application execution across different computing environments. In the case of Docker, a container image is a compact, standalone package containing everything needed to run an application.

When Docker containers run on Docker Engine, they offer standardization for portability, are lightweight by sharing the host machine’s OS kernel, and provide robust security features. This ensures applications run uniformly, irrespective of differences between the OS and the infrastructures

How to verify whether docker is installed ?

The following command should display the installed Docker version, confirming that Docker is successfully installed.

discoveringsystems@Ubuntu:~$ docker --version
Docker version 20.10.22, build 3a2c30b

How to start the first docker container ?

Let’s start with a simple example using the official Nginx image. The following command can create the nginx container with port 80 mapped with the port 80 on the host machine which runs the docker container. Please note that docker will first check for available nginx images in the local machine and then will look for images externally in docker hub registry to pull it before running.

discoveringsystems@Ubuntu:~$  sudo docker run -d -p 80:80 --name mynginx nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
af207e955271: Pull complete
325ba2f03c3e: Pull complete
8c27d2ff7dfa: Pull complete

Digest: sha256:5bdc39f2f8ae9d8dc65ed00f2ee56d00386c6f8bc8a7b320d0a294d8e3b46026
Status: Downloaded newer image for nginx:latest
c5fbff025a118837b4b0837ea0af890e157b23a4e1d40f58e8a38ee4330943f0

Options used part of the command

-d: Run the container in the background (detached mode).
-p 80:80: Map port 80 on your host to port 80 on the container.
--name mynginx: Assigned name to your container .
nginx: The name of the Docker image in use

How to view the running container ?

You can execute the following command to display the running containers along with their IDs, names, and other details.

discoveringsystems@Ubuntu:~$ sudo docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                NAMES
c5fbff025a11   nginx     "/docker-entrypoint.…"   7 minutes ago   Up 7 minutes   0.0.0.0:80->80/tcp   mynginx

How to check whether the nginx service is running on the container ?

Open your web browser and navigate to localhost:80 . You should see the default Nginx welcome page.

How to stop a container ?

You can use the docker stop command to stop the container , you can either use the container-id or the assigned name for the container , both should work 

discoveringsystems@Ubuntu:~$ sudo docker stop mynginx
mynginx

How to check available stopped container ?

Stopped containers won’t be visible using the docker ps command , so we need to use docker ps -a to show all stopped containers


discoveringsystems@Ubuntu:~$ sudo docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

discoveringsystems@Ubuntu:~$ sudo docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                     PORTS     NAMES
c5fbff025a11   nginx     "/docker-entrypoint.…"   43 minutes ago   Exited (0) 6 seconds ago             mynginx

How to start the stopped container ?

You can use the docker start command to start the stopped containers using their container-id or assigned name 

discoveringsystems@Ubuntu:~$  sudo docker start  mynginx
mynginx

discoveringsystems@Ubuntu:~$  sudo docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS                NAMES
c5fbff025a11   nginx     "/docker-entrypoint.…"   52 minutes ago   Up 3 seconds   0.0.0.0:80->80/tcp   mynginx

Conclusion

In this article , we touched upon the basics of docker containers  and explored some docker commands to run , start , stop and to check the status of the containers .

Leave a Comment

Your email address will not be published. Required fields are marked *