Skip to content

Instantly share code, notes, and snippets.

@budiantoip
Last active October 19, 2022 14:32
Show Gist options
  • Save budiantoip/8381a6839ec0db1b0460e0b25ce9588b to your computer and use it in GitHub Desktop.
Save budiantoip/8381a6839ec0db1b0460e0b25ce9588b to your computer and use it in GitHub Desktop.
Dockerfile Snippets

Images

Check existing images

docker images

Get mongo version 4.1

docker pull mongo:4.1

Remove an existing image

docker image rm mongo:4.2.5

Container

Get running container

docker container ls

Get all (running + non-running) container

docker container ls -all

Create container with random name, expose port 27000, map to 27017

docker container create -p 27000:27017 mongo:4.2.5

Create container with specific name, expose port 27000, map to 27017

docker container create --name mongoserver1 -p 27000:27017  mongo:4.2.5

Run created container

docker container start mongoserver1

Enter the container via bash

docker exec -t -i web /bin/bash

Stop a running containers

docker container stop mongoserver1 mongoserver2

Remove a container

docker container rm mongoserver1 mongoserver2

Sample of Dockerfile contents

FROM golang:1.14.2

# COPY source target (the target is our docker image)
COPY main.go /app/main.go

CMD ["go", "run", "/app/main.go"]

Install php extensions

RUN docker-php-ext-install curl

Install and enable pecl extensions

RUN pecl install memcached \
    && docker-php-ext-enable memcached

Build an image from a Dockerfile, this has to be run in a folder containing the Dockerfile

Please pay attention to the last dot!

docker build --tag app-golang:1.0 .

Create a container for our built image!

docker container create --name app-golang -p 8080:8080 app-golang:1.0

Push our image to docker registry, first create a repository in the registry

https://hub.docker.com/repository/create

Then create a new image tag from the existing image

docker tag app-golang:1.0 docker_hub_username/app-golang:1.0

Then login to docker registry via terminal

docker login

Then use the push command displayed after creating the above repository

docker push docker_hub_username/app-golang:1.0

Push to docker registry

docker build --tag budiantoip/php-fpm:5.6.40-bitnami-dev .
docker push budiantoip/php-fpm:5.6.40-bitnami-dev

Logs

Check logs of an existing container

docker container logs app-golang

Environment Variables

Create a container and set the environment variables!

docker container create --name app-golang -p 8080:8080 -e NAME=Docker app-golang:1.0

Set another environment variables

docker container create --name app-golang -p 8080:8080 -e NAME=Docker -e APP=golang -e PASSWORD=rahasia app-golang:1.0

Inspect

Inspect a docker container

docker container inspect app-golang

Network

docker network --help
docker network create golang_network
docker network ls
docker network golang_network mongo
docker network golang_network redis
docker network connect golang_network app-golang

Check whether mongo is connected into golang_network

docker container inspect mongo

Restart any started docker containers after creating the network!

Volume

Create volume for mongo and assign the volume

docker volume create mongo_data

Attach the volume

docker container create --name mongoserver1 -p 27000:27017 -v mongo_data:/data/db mongo mongo:4.2.5

Or store it in /Users/username/Desktop/mongodb folder

docker container create --name mongoserver1 -p 27000:27017 -v /Users/username/Desktop/mongodb:/data/db mongo mongo:4.2.5

Docker Compose

rebuild the built container using docker-compose

docker-compose up -d --no-deps --build web

Prune

This will remove dangling components

docker container prune
docker image prune
docker volume prune
docker system prune

Get component's disk usage

docker system df

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment