Docker is a popular tool for packaging and deploying applications, and a key part of the Docker workflow is running Docker containers. In this beginner’s guide, we will explain what Docker containers are and how to run them.

What is a Docker Container?

A Docker container is a lightweight and standalone executable package that includes everything needed to run an application, including the code, a runtime, libraries, environment variables, and config files.

Containers are built on top of Docker’s open-source containerization technology, which allows developers to package and deploy applications in a container, making it easier to run applications consistently on different environments.

How to Run a Docker Container

To run a Docker container, you need to have a Docker image, which is a packaged application that can be deployed in a container. You can build a Docker image using the Dockerfile and docker build commands, or you can pull an image from a registry, such as Docker Hub.

Once you have an image, you can use the docker run command to run the container. The docker run command takes the following syntax:

## Syntax

docker run [options] IMAGE [COMMAND] [ARG...]

Here are some common options for the docker run command:

  • -d: Run the container in detached mode, which means it will run in the background.
  • -p: Publish a container’s port to the host. For example, -p 8080:80 will expose port 80 in the container as port 8080 on the host.
  • -e: Set an environment variable in the container. For example, -e MY_VAR=value will set the MY_VAR environment variable to value in the container.
  • --name: Assign a name to the container.

Here’s an example of how to run a Docker container using the docker run command:

docker run -d -p 8080:80 --name my-web-server nginx 

This command will run the nginx image in detached mode, exposing port 80 in the container as port 8080 on the host, and giving the container the name my-web-server.

Running Multiple Containers

You can run multiple containers using the docker-compose tool, which allows you to define and run multi-container applications in a single command. To use docker-compose, you need to create a docker-compose.yml file that defines the containers and their dependencies.

Here’s an example docker-compose.yml file that runs a web server and a database:

version: ‘3’

services:

  web:

    image: nginx

    ports:

       “8080:80”

  db:

    image: mysql

    environment:

      MYSQL_ROOT_PASSWORD: password

To run the containers defined in the docker-compose.yml file, you can use the docker-compose up command:

docker-compose up 

This will start the web server and database containers and run them in the foreground. To run the containers in detached mode, you can use the -d flag:

docker-compose up -d 

Stopping and Removing Containers

To stop a running container, you can use the docker stop command followed by the container name or ID. For example, to stop the my-web-server container, you can use the following command:

docker stop my-web-server 

To remove a stopped container, you can use the docker rm command followed by the container name or ID. For example, to remove the my-web-server container, you can use the following command:

docker rm my-web-server 

If you want to stop and remove all containers, you can use the docker container prune command. This will remove all stopped containers, as well as any networks and volumes that are not in use.

docker container prune 

Best Practices for Running Containers

Here are a few best practices to follow when running Docker containers:

  • Use the latest version of Docker: It’s important to keep Docker up to date to ensure that you have the latest features and security patches.
  • Use the right base image: Choose a base image that is appropriate for your application, such as an operating system image or a language runtime image.
  • Use environment variables: Instead of hardcoding values in the Dockerfile or docker-compose.yml file, use environment variables to make the container more flexible and easier to modify.
  • Use volumes: Volumes allow you to persist data outside of the container and make it available to other containers. This can be useful for storing data that needs to be preserved, such as a database.
  • Use the `--restart` option: The `--restart` option allows you to specify the behavior of the container when it exits. For example, you can use the --restart always option to automatically restart the container if it exits.
  • Use resource limits: You can use the `--cpus` and `--memory` options to limit the resources that a container can use, which can help prevent resource contention and improve performance.

By following these best practices, you can run Docker containers efficiently and effectively.

Conclusion

In this guide, we have covered the basics of running Docker containers and provided some best practices to follow. By using the docker run and docker-compose commands, you can easily deploy and manage containers in your environment.