★ WELCOME ★

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
articles:linux:docker-reference-page [2022/02/12 14:01]
tom
articles:linux:docker-reference-page [2022/02/12 16:32] (current)
tom [Packaging & Distributing Your Own Docker Containers into Images]
Line 9: Line 9:
 **Docker Tutorial for Beginners [FULL COURSE in 3 Hours]\\ **Docker Tutorial for Beginners [FULL COURSE in 3 Hours]\\
 by [[articles:linux:personalities:TechWorld with Nana|TechWorld with Nana]] via Youtube** by [[articles:linux:personalities:TechWorld with Nana|TechWorld with Nana]] via Youtube**
 +
 +===== Base Questions =====
  
 What does a Docker structure look like? What does a Docker structure look like?
Line 22: Line 24:
   * Docker is less resource hungry because Docker containers use the host Linux kernel.   * Docker is less resource hungry because Docker containers use the host Linux kernel.
  
-Where are docker containers stored?+Where are docker images stored?
  
   * I think the big public repository is hub.docker.com.    * I think the big public repository is hub.docker.com. 
Line 28: Line 30:
 How to install Docker? How to install Docker?
  
-  * This can change, so get the latest information by doing an Internet search "Install Docker" and go to the official docker documentation. At this timeit's located at docs.docker.com.  +  * This can change, so get the latest information by doing an Internet search "Install Docker" and go to the official docker documentation. Alsoyou want to check with your distribution as well, because when I went to install Docker using the official docker documentation for Ubuntu I ran into some problems
-  * As of 12 Feb 2022, Linux Mint 20.3 is based on Ubuntu 20.04 "Focal". Try [cat /etc/upstream-release/lsb-release] in the terminal or visit https://en.wikipedia.org/wiki/Linux_Mint#Ubuntu-based_editions to verify your upstream distribution. Be careful if you are using Linux Mint LMDE, which is downstream of Debian, not Ubuntu.+  * Keep in mind that there are two base flavors of Mint. Linux Mint (based on Ubuntu) and Linux Mint LMDE (based on Debian)And your Mint is based upon a particular edition of the upstream repositories... So, you want to follow the Docker install instructions for your distribution edition or the upstream edition your Mint is based on. Try [cat /etc/upstream-release/lsb-release] in the terminal or visit https://en.wikipedia.org/wiki/Linux_Mint#Ubuntu-based_editions. 
 + 
 +===== Basic Commands ===== 
  
 <code> <code>
Line 42: Line 47:
 docker pull [name-of-image]  docker pull [name-of-image] 
  
-Start a container based on a specific version of image (pulls image from hub if not already downloaded to your machine: +Initial start of a container based on a specific version of image (pulls image from hub if not already downloaded to your machine: 
-docker run [flags] [name-of-image or id-of-image]:[version] +docker run [flags] [name-of-image or id-of-image][:version] 
   -d = runs in detached mode (so you can continue to use your terminal instance.   -d = runs in detached mode (so you can continue to use your terminal instance.
-  -p = port binding [port host]:[container port] or rather host's bare metal port bound to application's port (i.e. -p6000:6000)+  -p[host port]:[container port] = port binding the host's bare metal port and binding it to the application's port (i.e. -p6000:6000) 
 +  --name [user assigned container name] = assign your container a name (i.e. --name mydockercontainer) 
 + 
 +# Start a container that has already been run: 
 +docker start [container name or last 12 positions of container id] 
  
 # Stop a container: # Stop a container:
Line 53: Line 62:
 docker logs [container name or last 12 positions of container id]  docker logs [container name or last 12 positions of container id] 
  
 +# To enter into a terminal of a running container
 +docker exec -it [container name or last 12 positions of container id] /bin/bash
 +
 +# To list Docker networks:
 +docker network ls
 +
 +</code>
 +
 +===== Docker Networks =====
 +
 +In order for containers to talk to each other you have to setup a docker network. Normally, unless you are a developer, you would not have to do this, but if you do then review Nana's video, Developing with Containers chapter below. Or reference a more up-to-date tutorial or documentation. However, you can also setup a Docker network automatically when you use Docker compose. See topic below.
 +
 +{{youtube>3c-iBn73dDE?medium&start=4208&end=5387}}
 +
 +===== Docker Compose =====
 +
 +Placeholders are in square [] brackets and comments are in curly {} braces in the template below. Note that in the template I have laid out 2 different Docker containers in a single compose file. When you do this, a common Docker network is created. Also, indentation must be precise for this to work (supposedly).
 +
 +<code>
 +version:'3' {This is the version of Docker Compose the script is for.}
 +services:
 +  [user assigned name]: {same as docker run --name flag}
 +    image: [image name][:version] {name of image from Docker hub}
 +    ports:
 +      - [host port]:[container application port]
 +    volumes:
 +      - [user reference name]:[container path] {for a "named" volume}; or
 +      - [host path]:[container path] {for a "defined" volume}; or
 +      - [container path] {for an anonymous volume}
 +    environment:
 +      - [environment setting]=[assigned value]
 +  [user assigned name]: {same as docker run --name flag}
 +    image: [image name][:version] {name of image from Docker hub}
 +    ports:
 +      - [host port]:[container application port]
 +    volumes:
 +      - [user reference name]:[container path] {for a "named" volume}; or
 +      - [host path]:[container path] {for a "defined" volume}; or
 +      - [container path] {for an anonymous volume}
 +    environment:
 +      - [environment setting]=[assigned value] 
 +
 +# For two containers to share a volume, you can set that after defining named volumes above and adding the volume after the container definition and using an already defined name (i.e).
 +
 +volumes:
 +  db-data: {assuming db-data was established above}
 +    driver: local {this is needed but not sure why}
 +
 +</code>
 +
 +The contents of the file above would be saved in a text file saved with a .yaml extension.
 +
 +To start a Docker compose file, run:
 +
 +<code>
 +docker-compose [flags] [command]
 +  -f [name of yaml file] 
 +
 +Commands:
 +
 +  up = start a Docker Compose configuration
 +  down = stop a Docker Compose configuration
 +
 +Examples:
 +
 +docker-compose -f mycontainers.yaml up
 +docker-compose -f mycontainers.yaml down
 +</code>
 +
 +===== Persistent Data with Docker Volumes =====
 + 
 +You need to do this when you want to store data/files between restarts of a Docker container. 
 +
 +You have three types of Docker volumes, i.e.
 +
 +<code>
 +# A defined volume (Note: I am only showing the -v flag).
 +docker run -v [host folder]:[container folder]
 +docker run -v /home/myvolume/data:/var/lib/mysql/data
 +
 +# An anonymous volume (Note: I am only showing the -v flag and I don't necessarily know where the persistent storage is kept but should be below /var/lib/docker/volumes).
 +docker run -v [container folder]
 +docker run -v /var/lib/mysql/data
  
 +# A named volume (Note: I am only showing the -v flag, again I may not know where the storage is kept but should be below /var/lib/docker/volumes, plus I am able to reference it better than an anonymous volume).
  
 </code> </code>
  
 +===== Packaging & Distributing Your Own Docker Containers into Images =====
  
 +Again, check with Nina on how to do this. Clients of Dockerized applications would not normally have to do any of this.
  
 +{{youtube>3c-iBn73dDE?medium&start=6120&end=8844}}
Print/export