Docker Container

Nidhi Ashtikar
5 min readApr 21, 2024

--

We are going to learn Docker in four main components, Getting started with Docker containers:

Learn Docker Architecture!

Imagine a Docker container as a small virtual computer that holds everything a piece of software needs to run. It’s like a lunchbox that has the food, utensils, and napkins all neatly packed inside.

Here’s a breakdown:

In simple terms, Docker containers are like magic lunchboxes for software — they hold everything you need and let you take it wherever you want to run it!

Docker Container Commands:

1. Creating Docker Container:

#This command create and runs the docker container 

docker container run <image> <command>
# -d : Run in Background

docker container run -d <image> sleep 30
# -it : Go inside the container 

docker container run -it <image> /bin/bash
#This command only create container but do not run it 

docker container create
#Allows you to execute a command inside a running Docker containe

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

docker exec -it my_container /bin/bash

# -d: Detach from the container's terminal after executing the command.
# -u <username>: Specify the user context in which to run the command.
# -e <variable>=<value>: Set environment variables inside the container.

2. Listing Docker Container:

#List docker container

docker ps

#List all containers running and stopped

docker ps -a

Alternative

#List docker container

docker container ls

#List all containers running and stopped

docker container ls -a

docker container ls -aq

3. Starts / Stops / Restart Container:

docker start <container>

docker stop <container>

docker restart <container>

4. Delete Docker Container:

docker rm [OPTIONS] CONTAINER [CONTAINER_ID]

docker rm <container_name_or_id>

docker rm container1 container2 container3

docker rm -f <container_name_or_id>

#-f : All data stored in the container's filesystem will be lost

docker container prune

#To delete all container

5. Information of the Docker Container: inspect

docker container inspect <container_ID>

6. What’s going on inside the container: Logs/ top/ stats

#To check the logs of the container 

docker container logs <container ID>
#To check process of the container 

docker container top <container ID >
#To check all static memory 

docker container stats

7. Container: Attach / Kill / Wait/ Pause/ Unpause

#To Kill the container 

docker container kill <container_ID>
#To Attach the container 

docker container attach <container_ID>
#Will Wait for the container to stop 

docker container wait <container_ID>
#To Pause container 

docker container pause <container_ID>
#To Unpause container 

docker container unpause <container_ID>

8. Port Forwarding:

#To check port of container 

docker container port <container_ID>
#To check ports 

netstat -nltp
# Forward a single port:

docker run -p <host_port>:<container_port> <image>
#Forward a range of ports:

docker run -p <host_port_start>-<host_port_end>:<container_port_start>-<container_port_end> <image>
#Bind to all interfaces (0.0.0.0)

docker run -p <host_ip>:<host_port>:<container_port> <image>

docker container run -p 8080:80 nginx

9. Copy a file from your local system to a Docker container:

docker cp <src_path> <container>:<dest_path>

docker cp /path/to/local/example.txt my_container:/path/to/container

10. Export and Import Docker Containers:

#Export a Docker container:

docker export <container> > <file.tar>

docker export my_container > my_container_export.tar

#This command exports the contents of the my_container container into a file named my_container_export.tar.

#Import a Docker container:

docker import <file.tar>

docker import my_container_export.tar

#This command imports the contents of the my_container_export.tar tar archive as a new Docker image. The image will be assigned a randomly generated name and ID.

11. Name a Docker container during creation:

docker run --name <container_name> <image>
docker run --name my_container nginx

12. Rename an existing Docker container:

docker rename <old_name> <new_name>
docker rename old_container_name new_container_name

13. Setting environmental variables:

#1. Set environmental variables during container creation:

docker run -e <variable>=<value> <image>

docker run -e MY_VAR=123 my_image
#2. Set environmental variables during container runtime:

docker exec -e <variable>=<value> <container> <command>

docker exec -e MY_VAR=456 my_container echo $MY_VAR
#3. Set multiple environmental variables:

docker run -e VAR1=value1 -e VAR2=value2 <image>

docker run --env-file <file> <image>

14. Update resources like CPU and memory limits for a running Docker container:

docker update [OPTIONS] CONTAINER [CONTAINER...]


#Updating CPU shares:

docker update --cpu-shares <value> <container>

docker update --cpu-shares 512 my_container
#Updating memory limit:

docker update --memory <value> <container>

docker update --memory 512m my_container

Additional options:

  • --memory-reservation: Specifies a soft limit on memory usage.
  • --memory-swap: Specifies total memory usage, including virtual memory swap.
  • --cpuset-cpus: Specifies which CPU cores the container can use.
  • --kernel-memory: Specifies kernel memory limit.
docker update --cpu-shares 512 --memory 512m my_container

15. Mount volumes or Bind mounts in a Docker container:

#Mounting a volume:

docker run -v <host_path>:<container_path> <image_name>

docker run -v /host/folder:/container/folder nginx
#Using --mount for more advanced options:

docker run --mount type=<mount_type>,source=<source_path>,target=<target_path> <image_name>


docker run --mount type=bind,source=/host/folder,target=/container/folder,readonly nginx

16. Connects the container to a specific Docker network:

docker run --network=<network_name> <image>

17. Links containers together for communication:

docker run --link <container_name>:<alias> <image>

18. Saves the current state of a container as a new image:

docker commit <container> <new_image>

17. Shows the changes made to the filesystem of a container:

docker container diff <container>

18. Runs a command as a specific user in a container:

docker container exec -u <user> <container> <command>

19. CheckPoints:

#Creates a checkpoint of a running container's state.

docker checkpoint create <container> <checkpoint_name>
#Restores a container to a previously created checkpoint.

docker checkpoint restore <container> <checkpoint_name>

20. Creates a Docker service for managing containers in a swarm mode cluster.

docker service create <image>

21. Shows the status of a container (running, paused, exited, etc.).

docker inspect --format='{{.State.Status}}' <container>

If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!

Git Hub : github.com/nidhi-ashtikar

Thanks for spending your valuable time learning to enhance your knowledge!

--

--

Nidhi Ashtikar
Nidhi Ashtikar

Written by Nidhi Ashtikar

Experienced AWS DevOps professional with a passion for writing insightful articles.

Responses (2)