Here some useful shortcuts I found pretty useful for my daily work:
Main Commands
docker login
Show all active containers:
docker container ls
Show all containers
docker container ls -a
Remove specific container( use -f to force remove actively running container)
docker container rm -f 81c77
Run a container with detach process(background) run without to run it activly
docker container run —-publish 80:80 —-detach —name cont_name nginx
Run tail logs
docker containers --logs
Run docker with env
docker container run --publish 3306:3306 --detach --name mysql_cont --env MYSQL_RANDOM_ROOT_PASSWORD=yes mysql
80:80 - First goes to Docker host, second to container
To see a docker config file for specific container use:
docker container inspect exciting_thompson
To check performance for all active containers use:
docker container stats
To get access ti Docker container terminal(imulate ssh)
docker container run -it --name proxy nginx bash
Connect to already running container
docker container exec -it mysql bash
Connect to already created but currently not running:
docker container start -ai ubuntu
CLEAN UP ALL:
docker system prune -a
COMPOSE
Compose file example:
version: "3"
services:
# Create a service named db.
db:
# Use the Docker Image postgres. This will pull the newest release.
image: "postgres"
# Give the container the name my_postgres. You can changes to something else.
container_name: "my_postgres"
# Setup the username, password, and database name. You can changes these values.
environment:
- POSTGRES_USER=john
- POSTGRES_PASSWORD=pwd0123456789
- POSTGRES_DB=mydb
# Maps port 54320 (localhost) to port 5432 on the container. You can change the ports to fix your needs.
ports:
- "54320:5432"
# Set a volume some that database is not lost after shutting down the container.
# I used the name postgres-data but you can changed it to something else.
volumes:
- ./postgres-data:/var/lib/postgresql/data
Creating it in docker:
docker-compose up -d
To run with attached
docker-compose up
Build an image:
docker image build -t fatemes_app:2.0 .
To run a command:
docker-compose run web rake db:create