Skip to content

4.1 Understanding Docker Containers

What are Docker Containers?

Docker containers are like individual compartments that hold your applications and everything they need to work correctly. They are super lightweight and don't take up much space, making them easy to move around.

Using Docker containers ensures that your software runs consistently no matter where it's deployed. Learning how to create and run Docker containers is really important because it lets you take full advantage of containerization and all its benefits, like easily moving your apps between different systems.

Creating and Running Docker Containers

Creating a Container:

  • To create a Docker container, you need an image as the basis. Images serve as blueprints for containers, containing all the necessary files and configurations.
  • You can create a container from an existing image or build a custom image using a Dockerfile (as we discussed earlier).

Running a Container:

  • To run a container, you can use the docker run command followed by the image name. For example:
docker run image-name
  • Docker will search for the specified image locally. If it doesn't find it, it will automatically pull the image from a registry (like Docker Hub) unless you specify a specific registry.
  • The docker run command starts a new container based on the specified image. By default, it runs the container in the foreground, displaying the container's output in the terminal.

Container Lifecycle:

  • When you run a container, it goes through various stages in its lifecycle. Initially, the container is created, and then it is started.
  • While the container is running, you can interact with it, and it can communicate with the outside world.
  • To stop a running container, you can use the docker stop command followed by the container ID or name. For example:
docker stop container-id
  • You can also restart a stopped container using the docker start command followed by the container ID or name.

Container Management:

  • Docker provides various commands to manage your containers effectively.
  • Use the docker ps command to list all running containers. Adding the -a option displays all containers, including those that are stopped.
  • To remove a container, you can use the docker rm command followed by the container ID or name. For example:
docker rm container-id
  • You can also use the docker rm -f command to forcefully remove a running container.

Container Networking:

  • By default, containers can communicate with each other on the same Docker network using their IP addresses.
  • Docker provides network options to control container networking, such as creating custom networks, assigning static IP addresses, and linking containers together.
  • You can also publish container ports to the host machine, allowing external access to services running inside the container.