Docker Commands

Action Command
DOCKERFILE # Dockerfile instructions are used to create (custom) Docker Images.
# Steps to create a Dockerfile.


STEP-1: Navigate to your project directory.
cd path/to/your/project

STEP-2: Create & Edit your Dockerfile (You can also use your favourite editor)
Important: No file extenstion required

vi Dockerfile

STEP-3: Example instructions to a Dockerfile
Important: Below example runs an HTML project in docker container

# Base image.
FROM nginx:alpine

# Copy all HTML project files to the container.
COPY . /usr/share/nginx/html

# Expose container ports.
EXPOSE 8181

STEP-4: Build your custom image using this Dockerfile and run (See Build Image & Run Image section)
docker build -t <image_name> .

IMAGES # List Images.
docker images
docker image ls

# Pull Images from docker-hub, this will save image to our system [local docker].
docker pull <image>:<tag>
docker pull hello-world
docker pull ubuntu:18.04

# Get Image detailed information.
docker image inspect <image>:<tag>
docker image inspect hello-world

Note: Docker will consider latest tag if you do not mention the tag.
Also, ‘docker pull ubuntu’ instructs docker to pull an image named 'ubuntu' from the official Docker Hub.
This is simply a shortcut for the longer docker pull docker.io/library/ubuntu command.
Generally, you can find images here https://hub.docker.com/_/<image>.
SEARCH IMAGES # Search docker-hub images.
docker search <string>
docker search busy

# With minimum 5 stars.
docker search --filter stars=5 busy

Note: This will result image name or tag name starting with given string.
RUN IMAGES # Run Image in a new Container.
docker run <image>
docker run busybox

# Run Image and remove Container once Container exixts.
docker run --rm <image>
docker run --rm busybox

Run container in background and print container ID
-d --detach to detach terminal so that you can further use your terminal otherwise, it has ‘Nginx is running...’

Publish a container's port(s) to the host
-p tcp works on 80, 8888 will be recognised as tcp 80

-—name container name

-t Allocate a pseudo-TTY

$ docker run -p 8888:80 prakhar1989/static-site # Container terminal

docker run -it busybox sh

exit command to exit

CONTAINERS # List running container.
docker container ls
docker ps

# List all containers including running and exited.
docker container ls -a
docker ps -a

# List (only IDs) of containers, quite mode.
docker container ls -aq
docker ps -aq

# Stop running containers.
docker stop <container_id>
docker stop <container_id1> <container_id2>

# Start stopped containers.
docker start <container_id>
docker start <container_id1> <container_id2>

# Restart containers.
docker restart <container_id>
docker restart <container_id1> <container_id2>

# Delete container.
docker rm <container_id>
docker rm <container_name>
docker rm <container_id1> <container_id2>

#WARNING! This will remove all stopped containers.
docker container prune

# Delete containers (forcefully).
docker rm -f <container_id>

# Delete all exited containers.
docker rm $(docker ps -aq status=exited)

# Rename container.
docker rename <container_name> <new_name>

INTERACT WITH CONTAINERS # Get terminal of container.
docker exec -it <container_id> /sh
docker exec -it <container_id> /bin/bash

HOST & CONTAINER # Copy file from Host to Container.
docker cp <container_id>:/path/to/source /path/to/destination

# Copy file from Container to Host.
docker cp /path/to/source <container_id>:/path/to/destination

VOLUME # Inspect Volume.
docker volume inspect <volume_name>

# Mount all volumes used in C1 to C2.
docker run -it —name <container_id2> —volumes-from <container_id1> bash

Comments

Drupal Contribution
Git Commands
RESTful Services
Lando Commands
Docker Commands
MySQL
Database Quick Code
Drush Commands
Drupal Console
PHP Quick Code
Drupal Quick Code
Composer Commands
Linux Commands
Linux Shell Scripting
Drupal Hooks
Twig Tricks
PHPUnit Test
PhpMyAdmin
Drupal Constants
CSS Clues
BLT Commands
Vagrant Commands
Localhost
127.0.0.1
Drupal Interview
Drupal Certifications
Concept & Definitions
Mac Tips
Windows Tips
Browser Tips

Best Practice

Use 'elseif' instead of 'else if'
#CodingTips

As of PHP 5.4 you can also use the short array syntax, which replaces array() with []
#CodingTips

Functions in general shall be named using snake_case(say, my_function()), and using camelCase(say, myFunction()) when declared within a plugin class
#CodingTips

Variables in general shall be named using snake_case(say, $my_variable), and using camelCase(say, $myVariable) when declared within a plugin class
#CodingTips

Manage automatically assigning of new permissions whenever a module is enabled here- admin/config/people/accounts
#ConfigurationTips

Manage source of Main-menu and User-menu links here- admin/structure/menu/settings
#ConfigurationTips

Helper function(s) shall be named prefixing an underscore(say, _my_helper_function()), which can prevent hooks from being called
#CodingTips

Ideally, configuring of 'Private file system path' at admin/config/media/file-system should be located outside of your Drupal root folder(say, ../my_private_files)
#ConfigurationTips

You should be aware that uploading files as 'Private file' will slow down the process of loading the files as Drupal has to be bootstrapped for every file that needs to be downloaded
#ConfigurationTips #BeAware

Code should always be pushed up(dev -> staging -> production) and databases should only be pushed down(production -> staging -> dev)
#DevelopmentTips

Get Raw SQL Query of drupal dynamic queries before executing it using $query->__toString();
#DebugTips

In VI-Editor, Press ESC key to come in command mode and for undo type :U and for redo type :Ctrl+R
#LinuxTips

Insert queries must always use a query builder object(layer of abstraction), allowing individual database drivers special handling for column values (if applicable), example case for LOB and BLOB fields.
#DatabaseQueryTips

Drupal uses the .inc extension to prevent files from being executed directly.
#DevelopmentTips

Popular Posts