Docker: Pulling an image and running a container

Docker is a powerful tool that allows developers to easily package and deploy applications in a container. In this article, we will go over the basics of pulling an image from a registry and running it as a container.

What is Docker?

Docker is a platform that allows developers to easily create, deploy, and run applications in containers. Containers are lightweight, portable environments that include all of the necessary dependencies and libraries for an application to run. By using containers, developers can ensure that their applications will run consistently across different environments, without the need for manual configuration.

Docker Hub

Docker Hub is the default registry for storing and distributing Docker images. It is a centralized location where developers can store and share their images with the community. The images on Docker Hub are organized into repositories, which can be public or private.

Pulling an Image

To use an image, you first need to pull it from a registry. The most common way to pull an image is by using the “docker pull” command. For example, to pull the latest version of the “nginx” image, you would use the following command:

docker pull nginx

This will download the latest version of the “nginx” image from the Docker Hub registry.

Running a Container

Once you have pulled an image, you can run it as a container. The most common way to run a container is by using the “docker run” command. For example, to run the “nginx” image as a container, you would use the following command:

docker run --name my-nginx -p 80:80 nginx

This command will start a new container, named “my-nginx”, and map port 80 on the host to port 80 in the container. The “nginx” image will be used as the base for the container.

You can also start, stop and restart the container using the following command:

docker start my-nginx
docker stop my-nginx
docker restart my-nginx

Exposing Ports

The -p flag is used to expose ports from the container to the host. The format is -p host_port:container_port. For example, -p 80:80 would map port 80 on the host to port 80 in the container.

You can also see the running container using the following command:

docker ps

It will give you the list of running containers with their container name, image name and exposed ports.

Mounting Volumes

The -v flag is used to mount a host directory as a volume in the container. The format is -v host_dir:container_dir. For example, -v /var/www:/usr/share/nginx/html would mount the host directory /var/www as /usr/share/nginx/html in the container.

You can also see the list of volumes used by a container using the following command:

docker inspect my-nginx | grep -i "volumes"

Running a Command

The “docker run” command can also be used to run a specific command in the container. For example, to run the “ls” command in the “nginx” container, you would use the following command:

docker run --name my-nginx -p 80:80 nginx ls

This will run the “ls” command in the “nginx” container and display the output.

Running a Daemonized Container

By default, a container will stop running as soon as the command specified in the “docker run” command completes. However, you can also run a container in daemonized mode, which will keep it running in the background. To run a container in daemonized mode, you can use the “-d” flag with the “docker run” command. For example, to run the “nginx” image as a daemonized container, you would use the following command:

docker run -d --name my-nginx -p 80:80 nginx

This command will start the “nginx” image as a daemonized container, named “my-nginx”, and map port 80 on the host to port 80 in the container.

Viewing Logs

You can view the logs of a running container using the “docker logs” command. For example, to view the logs of the “my-nginx” container, you would use the following command:

docker logs my-nginx

This will display the logs of the “my-nginx” container in the terminal.

Removing a Container

You can remove a container using the “docker rm” command. For example, to remove the “my-nginx” container, you would use the following command:

docker rm my-nginx

This will remove the “my-nginx” container and any changes made to it.

Removing an Image

You can remove an image using the “docker rmi” command. For example, to remove the “nginx” image, you would use the following command:

docker rmi nginx

This will remove the “nginx” image from your system.

Conclusion

In conclusion, Docker is a powerful tool that allows developers to easily package and deploy applications in a container. By pulling an image from a registry and running it as a container, developers can ensure that their applications will run consistently across different environments. The above article provides the basic commands for pulling an image, running a container, exposing ports, mounting volumes, running a command, running a daemonized container, viewing logs, removing a container and removing an image.

0 Comments

Submit a Comment

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

Related Articles