Basics
Basic Commands
# | Command | Description |
1 |
| Build the image from the Dockerfile found in the project directory |
2 |
| Run built docker image with |
3 |
| List all the containers |
4 |
| Stop given docker image, based on it's name, for example: |
5 |
| This command will run local image, if note found it will pull it from DockerHub, e.g. |
6 |
| Show all processes (ps) |
7 |
| Access interactive section of Node instance, where you can run basic commands, node api, etc. |
8 |
| Shows all available option for command |
9 |
| Remove |
Containers
# | Commmand | Description |
---|---|---|
1 |
| Run container in the detached mode, this doesn't block your terminal so you can run different commands, but you can't see the output. |
2 |
| You can switch on the attach mode, which will show you the p=output in the console, so you can see printed statments, etc. Example use: |
3 |
| See the container's logs (you can see past logs), but stay in detached mode. Example: |
4 |
| See the logs and activate the attach mode (now you will be able to see the future logs). |
5 |
| Start container in the attach mode (you can see output). Example use: |
6 |
| Run container in interactive mode which allows STDIN, you can provide input to the terminal window. |
7 |
| Start the container in attach mode and with option allowing to input data in terminal (I want to input something mode). |
8 |
| Remove container that was previously stopped. You can pass multiple images names after rm, seperated by whitespace. |
9 |
| Allows to copy file into and from the container. Example use: |
10 |
| Copy files from container to local folder, example use: |
11 |
| Name the container, example use: |
12 |
| Run image based on name and tag, example use:
|
Images
# | Command | Description |
---|---|---|
1 |
| List all images. |
2 |
| Remove image and all its layers. Remember that container needs to be remove first for this command to work. Example use: |
3 |
| Remove all unused images. |
4 |
| Remove all images, including tagged images. |
5 |
| Run image on port 3000 in dettached mode and remove container when is being stopped. Example use: |
6 |
| It will output information about image in form of a json file. |
7 |
| Tag the image with |
8 |
| Change the image name (it actually make a clone instead of changing name), for example: |
9 |
| Login to Docker Hub |
10 |
| Logout from Docker Hub |
11 |
| Push image to your repo in Docker Hub, for example: |
12 |
| Pull images from Docker Hub (this will detch the latest image), for example: |
13 |
| This command shows the history of the image, what was done step by step and might expose some env variables. Example usage: |
Volumes and Bind Mounts
# | Command | Description |
---|---|---|
1 |
| Remove a given volume, for example: |
2 |
| Remove all unused local volumes |
3 |
| Run container in |
4 |
| Use bind mounts to map local folder on your machine to container app. In this way you can update code on your machine without re-creating the existing container. (shortcut for the full path: |
5 |
| Add anonymous volume |
6 |
| Creates annonymous volume that is attached to single container, survives container shutdown/restart, unless |
7 |
| Creates named volume - cannot be added to the Dockerfile. Created in general - not tied to any specific container. Thanks to that it survices shutdown/restart - removal via Docker CLI. Can be shared accross containers and re-used for same container (across restarts). |
8 |
| Creates Bind Mount. Location on host file system, not tied to any specific container. It survices container shutdown/restart - removal on host fs. Can be shared across containers and re-used for same container (across restarts). |
9 |
| Adding |
10 |
| Check extra info about the volume, examle usage: |
Example Dockerfile using Volumes
ARG and ENV Variables
ARG - available inside of the
Dockerfile
, not accessible in CMD or any application code, example usage:docker build --build-arg
ENV - available inside of the
Dockerfile
and in application code, set viaDockerfile
or--env
or-e
ondocker run
command, for example:
Example Dockerfile using ARG
Using ARG Command example
Summary
Containers can read and write data, but written data is lost if the container is removed.
Volumes are folders on the host machine hdd, managed by Docker, which are mounted into the container (made available, mapped). Can help with data storage. Volumes persist if container shuts down/restarts. A container can weite data into a volume and read data from it. We have: - Named Volumes - survuve container removal and can be used to store persistent data - Anonymous Volumes are attached to a container - they can be used to save (temporary) data inside the container
Bind Mounts - folders on the host machine which are specified by the user and mounted into containers like Named Volumes (but we know the path on the host machine). Can help with direct container interaction.
Build ARGuments and Runtime ENVironment variables - can be used to make images and containers more dynamic/configurable
Last updated