Featured Video Play Icon

How To Run a Docker Container

Docker is a powerful tool that allows users to easily deploy and run applications in containers. Containers are lightweight, standalone, and executable packages that contain everything an application needs to run, including code, libraries, dependencies, and runtime. They are isolated from the host system and from other containers, making them a convenient and secure way to package and distribute applications. In this article, we will discuss how to run a Docker container.

Lead Developer Career Guide Banner 02

To run a docker container, you will need to have Docker installed on your system. If you do not have Docker installed, you can download it from the official Docker website (https://www.docker.com/) or use a package manager to install it.

Once Docker is installed, you can run a docker container using the docker run command. The docker run command takes the following syntax:

docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]

The IMAGE[:TAG] argument specifies the image to use for the container. The TAG is optional and specifies a specific version of the image to use. If no TAG is specified, the latest version of the image will be used.

The COMMAND argument specifies the command to run when the container is started such as executing a startup script. This is optional and can be omitted if the image includes a default command.

The ARG arguments are optional and can be used to pass additional arguments to the command.

Before starting a container, you may want to first search for the image you want to use. You can do this using the docker search command, which searches the Docker Hub registry for images. For example, to search for an Ubuntu image, you can use the following command:

docker search ubuntu

This will return a list of available Ubuntu images, along with their names, descriptions, and tags. You can then use the name and tag of the image you want to use with the docker run command.

Once you have found the image you want to use, you can run a docker container using the docker run command. Here is an example of starting a docker container using the docker run command:

docker run -it ubuntu:20.04 bash

This command will start a new container based on the ubuntu:20.04 image and run the bash command inside the container. If the image is not found locally, it will be pulled from the registry. The -it option specifies that the container should run in interactive mode and allocate a pseudo-TTY. This is especially useful for debugging running containers and viewing logs.

You can use other options with the docker run command to customize the behavior of the container. Some common options include:

  • -d: Run the container in detached mode. This means that the container will run in the background and the command prompt will return to the host system.
  • -p: Map a port on the host system to a port inside the container. This allows you to access the application inside the container from the host system.
  • -v: Mount a volume inside the container. This allows you to persist data across container restarts and share data between the host system and the container.

For example, to run a container in detached mode and map port 8080 on the host system to port 80 inside the container, you can use the following command:

docker run -d -p 8080:80 nginx

This docker command starts a new container based on the nginx image and runs it in detached mode. The -d option specifies that the container should run in the background, while the -p option maps port 8080 on the host system to port 80 inside the container. This allows you to access the web server running inside the container from the host system on port 8080.

If you wanted to specify a bind mount inside the container, you can use the following command:

docker run -d -p 8080:80 -v /app:/foo nginx

When the host directory of a bind-mounted volume doesn’t exist, Docker will automatically create this directory on the host for you. In the example above, Docker will create the /app/ folder before starting your container.

There are many options to consider when you are running Docker containers and in this article, I listed the most common options that you are likely to use often. If you want to learn more options for running Docker containers, you can find the full list here.

How to Run a Docker Container – Online Course

Tactics and Tools for Troubleshooting Docker on Pluralsight
Tactics and Tools for Troubleshooting Docker – Online Course Available on Pluralsight

If you’re struggling with Docker errors, check out my Pluralsight course, Tactics and Tools for Troubleshooting Docker. This course provides developers with the skills they need to troubleshoot and fix the most common Docker errors. By the end of this course, you will have the skills you need to reduce the time it takes you to assess and fix issues. Click the link below to get started with a free trial today!

Categories: DevOps, DockerTags: