https://github.com/alfahami/hands-on-docker
A cheat sheet and a beginner friendly walk through docker and it myriad functionalities.
https://github.com/alfahami/hands-on-docker
alpine-linux dangling-containers dock-container docker docker-architecture docker-compose docker-containers nginx-image registry rest-api
Last synced: about 2 months ago
JSON representation
A cheat sheet and a beginner friendly walk through docker and it myriad functionalities.
- Host: GitHub
- URL: https://github.com/alfahami/hands-on-docker
- Owner: alfahami
- Created: 2021-05-15T10:10:54.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-07-25T19:42:48.000Z (almost 5 years ago)
- Last Synced: 2025-01-06T03:42:52.710Z (over 1 year ago)
- Topics: alpine-linux, dangling-containers, dock-container, docker, docker-architecture, docker-compose, docker-containers, nginx-image, registry, rest-api
- Language: JavaScript
- Homepage:
- Size: 272 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### DOCKER CHEAT SHEET
In this cheat sheet, I'll be using apache image and php.
At the end we'll build a **_multi-staged_** php mvc news app with Docker.
#### BASICS COMMANDS
* Container's ID
The CONTAINER ID is the first 12 characters of the full container ID printed out after running docker container run.\
The NAME is generated by docker and can be renamed by user later.
* Create a container with a custom name and publish it to a port\ without running.
```
docker create --pulblish | -p 8080:80 --name apache-server httpd
8fad9d84692858682f33ff2b60d3ee43e946af96d062c1fb3aa399ea082e5345
```
* Running a container (create and start)
```
docker container run -p 8080:80 --name apache-server httpd
# or we don't have to precise "container"
docker run -p 8080:80 --name apache-server httpd
```
* Naming and renaming a container
```
docker container run --detach --publish 8080:80 --name apache-server httpd
# Renaming
docker container rename
```
* Running a container in the background
```
docker container run --detach | -d -p 80800:80 --name apache-server
```
* Running a container interactively
```
docker container run -it | --interactive --tty
#
docker container run -it ubuntu
```
* Starting and restarting a container
```
docker container start apache-server |
apache-server
```
* Stopping a container (send a _SIGTERM_ signal)
```
docker container stop apache-server # or container ID
```
* Restarting a stopped container
```
docker container restart apache-server # or container ID
```
* Killing a container (send a _SIGKILL_ signal)
```
docker container kill apache-server # once killed it can't be restarted
```
* Running and stopping (_SIGTERM_) a container after Ctrl + C
```
docker container run --rm -it ubuntu
```
* Removing a stopped _SIGTERM_ container
```
docker container rm | -f
#
docker container rm apache-server
```
* Removing all dangling (stopped _SIGTERM_) containers
```
docker container prune
```
* Listing runnning containers
```
docker container ps | ls
```
* Remove unused data
```
docker system prune
```
Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.
* Listing running and stopped containers
```
docker container ps -a | --all | ls -a | ls --all
```
* Executing command inside a container (image configured to receive arguments)
```
docker container run --rm alpine uname a
Linux 094a0cbf37ad 4.19.0-16-amd64 #1 SMP Debian 4.19.181-1 (2021-03-19) x86_64 Linux
```
In the command above the uname -a is passed through the alpine image and get executed.
* Bind Mounts for executable images but not only
When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine.
```
docker run -d -it --name devtest --mount type=bind,source="$(pwd)"/target,target=/app nginx:latest
```
Using the --volume | -v flag.
```
docker run -d -it --name devtest -v "$(pwd)" target:/app nginx:latest
# this command will let us verify if the bind mount was created correctly
docker inspect devtest (container name)
```
* Inspecting a Docker object
Return low-level information on Docker Objects. It provides detailed information on constructs controlled by Docker.
```
docker inspect [OPTIONS] NAME[ID] [NAME | ID ...]
```
#### IMANGE MANIPULATIONS
According to the [officail docs](https://docs.docker.com/engine/reference/builder/)
Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.
* Creating images
```
# Make sure we're in the same folder as the Dockerfile
docker image buld -t build .
# will be used to run the built image.
# . indicate the build to be run from the current directory where Dockerfile exists.
```
* Our image should have httpd pre-installed
* The image should start apache server upon running
* Dockerfile
```
#base image
FROM debian:latest
RUN apt-get update
RUN apt-get install apache2 -y
RUN apt-get clean
COPY index.html /var/www/html/
EXPOSE 80
# running apache in foreground
CMD ["apache2ctl", "-D", "FOREGROUND"]
```
* Tagging an image (assigning a custom identifier to our image)
```
docker image build -t (--tag) :
# example
docker image build --tag custom-apache:packaged httpd:packaged .
```
* Changing the tag of an already tagged built image
```
docker image tag :
## or ##
docker image tag : :
#removing all existing images
docker image prune | --force (-f) | --all (-a)
```
* Show the history of an image
```
docker image history [OPTIONS: --human (-H) | --format | --no-trunc | --quiet (-q)] image
```