Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Running Your Custom Image | Working with Docker Images
Docker Essentials

bookRunning Your Custom Image

Running a Container from Your Custom Image

To run a container from your own custom image, use the docker run command. You need to specify the image name and, optionally, provide a name for the running container.

If you built an image called myapp:latest, start a container with:

docker run myapp:latest

Adding the --name flag allows you to assign a memorable name to the container, making it easier to manage:

docker run --name my-running-app myapp:latest

You can also run the container in the background (detached mode) by adding the -d flag:

docker run -d --name my-running-app myapp:latest

Key options for docker run:

  • Use --name to assign a custom container name;
  • Use -d to run the container in detached mode;
  • Specify your image and tag as image:tag (such as myapp:latest).

Running your containers with clear names and in the appropriate mode helps you manage and monitor them effectively.

Exposing Ports and Mounting Volumes When Running Containers

When running containers, you often need to expose ports and mount volumes to interact with your application and persist data.

Exposing ports is done with the -p flag, which maps a port on your host to a port inside the container:

  • Use -p <host_port>:<container_port> to map ports;
  • Access applications running inside the container from your host;
  • Run multiple containers on different host ports.

For example, to access a web application running on port 80 inside the container from port 8080 on your host:

docker run -p 8080:80 myapp:latest

Mounting a volume allows you to share files between your host and the container. Use the -v flag to specify the source and target directories:

  • Use -v /host/path:/container/path to mount directories;
  • Persist data generated by the container;
  • Share configuration files or code between host and container.

Example command:

docker run -v /host/path:/container/path myapp:latest

Combining options: You can run a container that is named, exposes ports, and mounts a volume:

docker run -d --name my-running-app -p 8080:80 -v /host/data:/app/data myapp:latest

This approach enables you to manage how your application communicates and where it stores data, providing flexibility in real-world deployments.

Troubleshooting Common Issues When Starting Custom Containers

If your custom container fails to start or behaves unexpectedly, use these steps to diagnose and resolve issues:

  • Check the container logs with the following command:
    docker logs <container-name>
    
  • Confirm that the image was built successfully and that the CMD or ENTRYPOINT in your Dockerfile is correct;
  • Verify that required ports are not already in use on your host;
  • If mounting volumes, ensure the source directory exists and has the correct permissions;
  • Run the container interactively for debugging by adding the -it flag and overriding the command:
    docker run -it myapp:latest /bin/sh
    
    This allows you to inspect the container's environment and troubleshoot issues directly.
question mark

What is the purpose of the --name flag in the docker run command?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 7.14

bookRunning Your Custom Image

Swipe to show menu

Running a Container from Your Custom Image

To run a container from your own custom image, use the docker run command. You need to specify the image name and, optionally, provide a name for the running container.

If you built an image called myapp:latest, start a container with:

docker run myapp:latest

Adding the --name flag allows you to assign a memorable name to the container, making it easier to manage:

docker run --name my-running-app myapp:latest

You can also run the container in the background (detached mode) by adding the -d flag:

docker run -d --name my-running-app myapp:latest

Key options for docker run:

  • Use --name to assign a custom container name;
  • Use -d to run the container in detached mode;
  • Specify your image and tag as image:tag (such as myapp:latest).

Running your containers with clear names and in the appropriate mode helps you manage and monitor them effectively.

Exposing Ports and Mounting Volumes When Running Containers

When running containers, you often need to expose ports and mount volumes to interact with your application and persist data.

Exposing ports is done with the -p flag, which maps a port on your host to a port inside the container:

  • Use -p <host_port>:<container_port> to map ports;
  • Access applications running inside the container from your host;
  • Run multiple containers on different host ports.

For example, to access a web application running on port 80 inside the container from port 8080 on your host:

docker run -p 8080:80 myapp:latest

Mounting a volume allows you to share files between your host and the container. Use the -v flag to specify the source and target directories:

  • Use -v /host/path:/container/path to mount directories;
  • Persist data generated by the container;
  • Share configuration files or code between host and container.

Example command:

docker run -v /host/path:/container/path myapp:latest

Combining options: You can run a container that is named, exposes ports, and mounts a volume:

docker run -d --name my-running-app -p 8080:80 -v /host/data:/app/data myapp:latest

This approach enables you to manage how your application communicates and where it stores data, providing flexibility in real-world deployments.

Troubleshooting Common Issues When Starting Custom Containers

If your custom container fails to start or behaves unexpectedly, use these steps to diagnose and resolve issues:

  • Check the container logs with the following command:
    docker logs <container-name>
    
  • Confirm that the image was built successfully and that the CMD or ENTRYPOINT in your Dockerfile is correct;
  • Verify that required ports are not already in use on your host;
  • If mounting volumes, ensure the source directory exists and has the correct permissions;
  • Run the container interactively for debugging by adding the -it flag and overriding the command:
    docker run -it myapp:latest /bin/sh
    
    This allows you to inspect the container's environment and troubleshoot issues directly.
question mark

What is the purpose of the --name flag in the docker run command?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
some-alt