Docker Containers
What is a Container?
Containers are an abstraction at the app layer that packages code and dependencies together. Multiple containers can run on the same machine and share the OS kernel with other containers, each running as isolated processes in user space. Containers take up less space than VMs (container images are typically tens of MBs in size), can handle more applications and require fewer VMs and Operating systems.

Containers are lightweight, portable, and efficient, making them ideal for deploying and scaling applications.
Why should you use Containers and not VMs?
- they share the host OS kernel and do not require a full OS per application.
- they are portable and can run on any machine that has the required container runtime installed.
- they are isolated from each other and from the host environment.
- they are lightweight and consume fewer resources than VMs.
- they rebuild quickly and efficiently when changes are made to the container images.
- you can easily communicate between containers and share data between them.

How to create and run a Container?
Creating a Container
To create a container, you will first need to create a Docker image, you can find more information about Docker images here.
Running a Container
Once you have a Docker image, you can run it as a container using the docker run command:
docker run --name myapp -d -p 8080:8080 myapp
--name myapp assigns a name to the container.
-d runs the container in detached mode to keep it running in the background.
-p 8080:8080 maps port 8080 on the host to port 8080 on the container.
To verify that the container is running, use the docker ps command or docker container ls which lists all running containers.
You should see your container listed in the output.
docker ps
To verify that your application is running correctly, you can access it through a web browser by navigating to http://localhost:8080.
Or you can execute curl http://localhost:8080 in the terminal to see the output. You should see the response from your application (in this case, "Hello, World!").
Stopping a Container
To stop a running container, you can use the docker stop command followed by the container ID or name:
docker stop myapp
This will stop the container and keep it in a stopped state. To remove the container, you can use the docker rm command followed by the container ID or name:
docker rm myapp
This will remove the container from your system and allow you to create a new container with the same name if needed.
Managing Containers
You can manage containers using various Docker commands, such as:
| Command | Description |
|---|---|
docker ps |
List all running containers. |
docker ps -a |
List all containers (including stopped ones). |
docker logs <container> |
Display the logs of a container. |
docker exec -it <container> bash |
Access the shell of a running container. |
docker inspect <container> |
Display detailed information about a container. |
docker stats <container> |
Display live performance data for a container. |
docker attach <container> |
Attach to a running container. |
docker pause <container> |
Pause a running container. |
docker unpause <container> |
Unpause a paused container. |
docker kill <container> |
Kill a running container. |
docker restart <container> |
Restart a running container. |
docker update <container> |
Update the configuration of a container. |
Next Step
Now that you have created and run a Docker container, you can explore more advanced topics such as Docker Volumes.