Docker: Difference between revisions

From Piszczynski
piszczynski>Aleks
No edit summary
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Install Docker Official==
<syntaxhighlight lang="bash"></syntaxhighlight>
Uninstall old versions:
<syntaxhighlight lang="bash">for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done</syntaxhighlight>


== Setup Docker ==
Install from apt repo:
[https://www.howtoforge.com/tutorial/ubuntu-docker-traefik-proxy/ Guide For setting up Traefik]
<syntaxhighlight lang="bash" line># Add Docker's official GPG key:
Install latest docker-ce:
sudo apt-get update
*sudo apt install docker-ce
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg


Start the docker service and enable it to launch everytime at system boot.
# Add the repository to Apt sources:
*systemctl start docker
echo \
*systemctl enable docker
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update</syntaxhighlight>


Check version of docker:
Install Docker Packages:
*docker version
<syntaxhighlight lang="bash">sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin</syntaxhighlight>


Additional: Running Docker for non-root user
== Setup Docker ==
Docker container can be run under the non-root user. We just need to add the user to the docker group.
Enable running docker commands without the use of sudo:
 
Add 'aleks' user.
 
*useradd -m -s /bin/bash aleks
Now add the 'aleks' user to the docker group, then restart the docker service.


*usermod -a -G docker aleks
Check Groups for docker group:
*systemctl restart docker
<syntaxhighlight lang="bash">cat /etc/group | grep docker</syntaxhighlight>


Test by running the docker hello-world.
Add if not found:
*docker run -it hello-world
<syntaxhighlight lang="bash">sudo groupadd docker</syntaxhighlight>


Install Docker Compose
Add user to docker group:
*sudo apt install docker-compose
<syntaxhighlight lang="bash">sudo usermod -aG docker $USER</syntaxhighlight>


Check version:
Configure to start on boot with systemd:
*docker-compose version
<syntaxhighlight lang="bash" line>sudo systemctl enable docker.service
sudo systemctl enable containerd.service</syntaxhighlight>


Create Custom Docker Network


Check the available docker network on the system.
*docker network ls


== Docker Commands ==
== Docker Commands ==
Line 43: Line 46:


Stop docker container
Stop docker container
*docker stop <container name>
<syntaxhighlight lang="bash">docker stop <container name></syntaxhighlight>


remove container
remove container
*docker rm <container name>
<syntaxhighlight lang="bash">docker rm <container name></syntaxhighlight>


run container
run container
*docker run --name <container name>
<syntaxhighlight lang="bash">docker run --name <container name></syntaxhighlight>


Stop all the containers
Stop all the containers
*docker stop $(docker ps -a -q)
<syntaxhighlight lang="bash">docker stop $(docker ps -a -q)</syntaxhighlight>


Remove all the containers
Remove all the containers
*docker rm $(docker ps -a -q)
<syntaxhighlight lang="bash">docker rm $(docker ps -a -q)</syntaxhighlight>
 
Enter shell of container<syntaxhighlight lang="bash">
docker exec -it <containerID> bash
</syntaxhighlight>
 
Show images already pulled by docker and stored locally<syntaxhighlight lang="bash">
docker images
</syntaxhighlight>
 


----
----
Line 63: Line 75:
== Docker High CPU usage ==
== Docker High CPU usage ==
See what containers are using system resources:
See what containers are using system resources:
*docker stats --all
<syntaxhighlight lang="bash" line>docker stats --all
*docker stats --all --format "table {{.ID}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
docker stats --all --format "table {{.ID}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"</syntaxhighlight>
 
 


Check specific container for process:
Check specific container for process:
*docker ps | grep <container id>
<syntaxhighlight lang="bash">docker ps | grep <container id></syntaxhighlight>
 


Check the logs for high cpu use container:
Check the logs for high cpu use container:
*docker logs --tail 100 <container Id>
<syntaxhighlight lang="bash">docker logs --tail 100 <container Id></syntaxhighlight>
 


Follow live logs:
Follow live logs:
*docker logs --follow <container ID>
<syntaxhighlight lang="bash">docker logs --follow <container ID></syntaxhighlight>
 
 
restart docker container:
<syntaxhighlight lang="bash">docker restart <container name></syntaxhighlight>
 
==Template docker-compose file==
[https://docs.docker.com/compose/compose-file/03-compose-file/ Docs]
 
Also see some other templates:[https://github.com/docker/awesome-compose awesome-compose github]
 
== Inspect Container by namespace ==
get process id of container:<syntaxhighlight lang="bash">
CONTAINER_PID=$(pgrep <containername> | sort | head -n 1) #gets PID of container
 
sudo lsns -p ${CONTAINER_PID} #gets all namespaces for the process
</syntaxhighlight>Info about the namespaces:
 
* mnt (''Mount'') - the container has an isolated mount table.
* uts (''UNIX Time-Sharing'') - the container is able to have its own hostname and domain name.
* ipc (''Interprocess Communication'') - processes inside the container can communicate via system-level IPC only to processes inside the same container.
* pid (''Process ID'') - processes inside the container are only able to see other processes inside the same container or inside the same pid namespace.
* net (''Network'') - the container gets its own set of network devices, IP protocol stacks, port numbers, etc.
* cgroup (''Cgroup'') - the container has its own ''virtualized view'' of cgroups (not to be confused with cgroups themselves).
 
== Copy Files into container ==
To copy files into/outof a running container use the docker cp command:<syntaxhighlight lang="bash">
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
</syntaxhighlight>
 
== Export file system of container ==
<syntaxhighlight lang="bash">
CONT_ID=$(docker create nginx:latest) #get container id
docker export ${CONT_ID} -o nginx.tar.gz # export container to tarball
 
docker export $(docker create nginx:latest) | tar -xC <dest> # oneliner to extract to destinatin folder
 


restart docker container
</syntaxhighlight>
*docker restart <container name>

Latest revision as of 18:58, 20 April 2024

Install Docker Official

Uninstall old versions:

for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done

Install from apt repo:

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Install Docker Packages:

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Setup Docker

Enable running docker commands without the use of sudo:

Check Groups for docker group:

cat /etc/group | grep docker

Add if not found:

sudo groupadd docker

Add user to docker group:

sudo usermod -aG docker $USER

Configure to start on boot with systemd:

sudo systemctl enable docker.service
sudo systemctl enable containerd.service


Docker Commands

Docker commands reference can be found here: docker command ref

Stop docker container

docker stop <container name>

remove container

docker rm <container name>

run container

docker run --name <container name>

Stop all the containers

docker stop $(docker ps -a -q)

Remove all the containers

docker rm $(docker ps -a -q)

Enter shell of container

docker exec -it <containerID> bash

Show images already pulled by docker and stored locally

docker images



When running docker containers the run command will require extra options for containers with specific uses. Environment vairables can be added to a document container on startup using a .env file and the switch "--env-file=<path to env file>" in the run command


Docker High CPU usage

See what containers are using system resources:

docker stats --all
docker stats --all --format "table {{.ID}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"


Check specific container for process:

docker ps | grep <container id>


Check the logs for high cpu use container:

docker logs --tail 100 <container Id>


Follow live logs:

docker logs --follow <container ID>


restart docker container:

docker restart <container name>

Template docker-compose file

Docs

Also see some other templates:awesome-compose github

Inspect Container by namespace

get process id of container:

CONTAINER_PID=$(pgrep <containername> | sort | head -n 1) #gets PID of container

sudo lsns -p ${CONTAINER_PID} #gets all namespaces for the process

Info about the namespaces:

  • mnt (Mount) - the container has an isolated mount table.
  • uts (UNIX Time-Sharing) - the container is able to have its own hostname and domain name.
  • ipc (Interprocess Communication) - processes inside the container can communicate via system-level IPC only to processes inside the same container.
  • pid (Process ID) - processes inside the container are only able to see other processes inside the same container or inside the same pid namespace.
  • net (Network) - the container gets its own set of network devices, IP protocol stacks, port numbers, etc.
  • cgroup (Cgroup) - the container has its own virtualized view of cgroups (not to be confused with cgroups themselves).

Copy Files into container

To copy files into/outof a running container use the docker cp command:

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Export file system of container

CONT_ID=$(docker create nginx:latest) #get container id
docker export ${CONT_ID} -o nginx.tar.gz # export container to tarball

docker export $(docker create nginx:latest) | tar -xC <dest> # oneliner to extract to destinatin folder