logo

Maycke's IT Guides

	  
##################################### DOCKER #############################################
~Docker is service that allow you to package and run applications in isolated containers
~ that share the same kernel.

~Docker CE (Community Edition) is the free open-source version that runs on Linux.
~Docker EE (Enterprise Edition) is the paid version, necessary to run on Windows or MacOS.

# this guide is about Docker CE version (the free one...)


#### basic docker configuration:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### you have 2 options to install docker:

## [option 1] from Ubuntu repositories (easier to install, but usually older version):
apt install docker.io

## [option 2] 
# add docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# add the repository to apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] 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 the latest version (use this same command when you need to update docker, later on):
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

#### docker test (to verify installation success):~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# test the installation by running the hello-world image: this command downloads a test 
#  image and runs it in a container. When the container runs, it prints a confirmation message
#  and exits:
sudo docker run hello-world

# (optional) you may also verify docker status:
systemctl status docker
#########################################################################################

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ EXTRAS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


##### EXAMPLE of SEARCHing for and INSTALLing one application in a container~~~~~~~~~~~~~~

## use the command docker search and the name of the desired packet so that you obtain a
##  list of possibilities, ex.:
docker search nginx

## to download a docker image, the tag 'latest' is the default:
docker pull nginx

## check your docker images (it brings the information 'IMAGE ID'):
docker images

## create container:
##  command model: docker run --name NAME -d -p 80:80 IMAGE_ID
##  instead of the IMAGE ID, you may also use the name of that image, example using IMAGE ID:
docker run --name nginxtest -d -p 80:80 7383c266ef25

#if you don't give it a name, a random one is given. The name can also be used to interact 
# with the container in other commands!!
#the port port at -p is the localhost_port:container_port

## at this point, the container should be working, nginx default page should be reachable!


##### Commands to MANAGE your CONTAINERS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# view containers (it brings the information 'CONTAINER ID'):
docker ps                                                     #running
docker ps -a                                                  #all

# to stop a container:
docker stop CONTAINER_ID

# to start a container (already created):
docker start CONTAINER_ID

# to remove a container (already stopped):
docker rm CONTAINER_ID


##### How to EDIT some CONFIGURATIONS of the EXAMPLE APPlication in the container~~~~~~~~~

## back to the nginx example, to edit the content of the page you may add the files to the
##  image before creating the container, OR specify the path of the volumes/directories
## in which the container should look for those files using the -v tag. Ex:
docker run --name docker-nginx -p 80:80 -d -v /home/username/docker-nginx/html:/usr/share/nginx/html nginx

#the content before the ':' is the path in your server, while the content after the ':' is
# the symbolic link from where nginx would look for the file. You can now add your webpage's
# files to that directory and nginx will update the content automatically.

## to edit the configurations from the nginx application, you may repeat the same steps you 
##  you did for the webpage's contents, but with the default nginx's configuration file:
## first, go to the root directory of your project, then copy nginx's configuration file to it:
cd /home/username/docker-nginx
docker cp docker-nginx:/etc/nginx/conf.d/default.conf default.conf

## edit that file according to what you want to change:
nano default.conf                      #ex.: switch the container's port to 666

## create a new container with the new configuration:
docker run --name docker-nginx2 -p 666:666 -v /home/username/docker-nginx/html:/usr/share/nginx/html -v /home/username/docker-nginx/default.conf:/etc/nginx/conf.d/default.conf -d nginx
#in this case I also wanted the nginx's port on my server to be 666

#now, whenever you change the configuration file /home/username/docker-nginx/default.conf, 
# restart the docker container to enable them:
docker restart docker-nginx


##### How to use DOCKER CP to LEARN containers' DIRECTORIES STRUCTURE~~~~~~~~~~~~~~~~~~~~~

## if you know the default configuration path of the application you are running in a container
##  (like in the docker-nginx container from the last example, it should be in /etc/nginx), you 
##  could copy that directory and learn where the files that interests you are:
docker cp docker-nginx:/etc/nginx/ .


##### How to SAVE the CHANGES made in a container to a NEW DOCKER IMAGE~~~~~~~~~~~~~~~~~~~

## Use this syntax:
docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name
#syntax explanation:
# "What you did to the image": is a description of what you did to the image
# repository: is usually the Docker Hub username
## example:
docker commit -m "port 666 and html files out of container" -a "username" fdf87f2a919e username/nginx-666

# after that, the new image will be available on your docker images:
docker images
#########################################################################################