Tips and tricks for docker
Most used docker commands for everyday
- Docker help
$ docker -h
- To check docker version
$ docker --version
- Login to docker
$ docker login
- To see list of images
$ docker image ls
- List of Containers
$ docker container ls
- See list of running containers
docker ps
- Restart a container
docker restart <container name>
- To stop docker container
$ docker stop <container id>
- Remove all docker images
docker rmi $(docker images -q)
- Stop all docker containers
docker stop $(docker ps -a -q)
Remove all docker containers
docker rm $(docker ps -a -q)
Deleting no longer needed containers (stopped)
docker container prune
Deleting no longer needed images
docker image prune
Delete all volumes, which are not used by any existing container
docker volume prune
Same for unused networks
docker network prune
- To run docker composer
$ docker-compose up
- Build a docker image
Syntax:
$ docker build -t <any tag name> <directory of the docker file>
Example:
$ docker build -t aspnetcore/generator .
Explanation:
Here aspnetcore/generator is tag name and '.' means current directory
- To run a docker image
docker run --rm -it -p 8000:80 mcr.microsoft.com/dotnet/core/samples:aspnetapp
The sample asp.net core application will run on http://localhost:8000. Here, 8000 is host machine port and 80 is container port.
- SSH into container
docker exec -it <container-id> sh
>>ls (Type ls to see all files and directory)
>>exit (type exit to return from the container)
- View information about your container using command
PS C:\Users\mahedee> docker exec -it b73cb9467f5a sh # cd .. # ls app bin boot dev etc home lib lib64 media mnt opt proc remote_debugger root run sbin src srv sys tmp usr var # cd app # ls Controllers Dockerfile HRM.API.csproj HRM.API.csproj.user Program.cs Properties WeatherForecast.cs appsettings.Development.json appsettings.json bin obj # cd .. # ls app bin boot dev etc home lib lib64 media mnt opt proc remote_debugger root run sbin src srv sys tmp usr var # cd src # ls HRM.API HRM.API.sln bin docker-compose.dcproj docker-compose.override.yml docker-compose.yml obj # cd bin # ls Debug # cd debug # ls HRM.API.deps.json HRM.API.exe HRM.API.runtimeconfig.json Swashbuckle.AspNetCore.Swagger.dll Swashbuckle.AspNetCore.SwaggerUI.dll appsettings.json HRM.API.dll HRM.API.pdb Microsoft.OpenApi.dll Swashbuckle.AspNetCore.SwaggerGen.dll appsettings.Development.json #
- Run a command in a running container
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Example
$ docker exec -ti 34717 sh -c "echo a && echo b"
or
$ docker exec -ti my_container sh -c "echo a && echo b"
or
$ docker exec -ti my_container sh -c "dir"
- To run bash in a container
Syntax:
$ docker exec -it <container id> bas
Example:
$ docker exec -it 2f7 bash
View information about your container user Visual Studio
- Use Ctrl+Q to activate Visual Studio Search Box, and type containers.
- Or, Go to View > Other Windows > Containers.
For more information see Use the Containers window
- Mounting a folder name ‘api’ into the container
$ docker run --rm -it -v ${PWD}:C:\api mcr.microsoft.com/dotnet/core/runtime:3.1
Here ${PWD} means current directory of host machine map to the directory C:\api into the container
- To inspect network and other information of a container
$ docker inspect <container id>
- Run a docker image
Syntax:
$ docker run --rm -it -p <host port>:<image port> <any tag name>
Example:
$ docker run --rm -it -p 8777:80 test2
Explanation:
Here test2 is tag name of the image which already running
- Login to docker and push an image
$ docker login
$ docker push mahedee/simpleapi
Explanation:
Here mahedee/simpleapi is the tag name of the image
- Setting the API Port with an Environment Variable inside a container
$ export ASPNETCORE_URLS=http://+:80
- To see docker event in json format
$ docker events --format ''
Tips:
Run this command in another windows
- To detach docker compose file
$ docker-compose up -d
- To see the log while running docker compose file
$ docker-compose logs -f
- Stop and remove containers, networks, images, and volumes
$ docker-compose down
- Remove one or more images
$ docker image rm <repository>:<tag>
Example:
$ docker image rm mahedee/webapi:build
__