Skip to main content

Lab #17: Run Command

Run command help to run a one-time command against a service. docker-compose run will start a new container to execute the command, not executed against a running container

Usage:
docker-compose run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] [-l KEY=VALUE...]
SERVICE [COMMAND] [ARGS...]

Options:
-d, --detach Detached mode: Run container in the background, print
new container name.
--name NAME Assign a name to the container
--entrypoint CMD Override the entrypoint of the image.
-e KEY=VAL Set an environment variable (can be used multiple times)
-l, --label KEY=VAL Add or override a label (can be used multiple times)
-u, --user="" Run as specified username or uid
--no-deps Don't start linked services.
--rm Remove container after run. Ignored in detached mode.
-p, --publish=[] Publish a container's port(s) to the host
--service-ports Run command with the service's ports enabled and mapped
to the host.
--use-aliases Use the service's network aliases in the network(s) the
container connects to.
-v, --volume=[] Bind mount a volume (default [])
-T Disable pseudo-tty allocation. By default `docker-compose run`
allocates a TTY.
-w, --workdir="" Working directory inside the container

Pre-requisite:

Tested Infrastructure

Platform Number of Instance Reading Time
Play with Docker 1 5 min

Pre-requisite

  • Create an account with DockerHub
  • Open PWD Platform on your browser
  • Click on Add New Instance on the left side of the screen to bring up Alpine OS instance on the right side

Assignment

  • Create a docker-compose.yml file
  • Bringup the containers

Create a docker-compose.yml file

version: '3.7'
services:
#Nginx Service
webserver:
image: nginx:alpine
container_name: Nginx
restart: unless-stopped
ports:
- "80:80"
- "443:443"
dbserver:
image: mysql:5.7
container_name: Mysqldb
restart: unless-stopped
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: Pa$$w0rd
MYSQL_USER: test
MYSQL_PASSWORD: Pa$$w0rd123
MYSQL_DATABASE: test
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:

Bringup the containers

$ docker-compose up -d

Checking container status

$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------
Mysqldb docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp, 33060/tcp
Nginx nginx -g daemon off; Up 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp

Listout the services

$ docker-compose ps --services
webserver
dbserver

Start webserver service container with shell

$ docker-compose run webserver /bin/sh

Contributor

Savio Mathew

Next >> Lab #18: Scale Command