Merge pull request #70 from StefanScherer/powershell

Show multiline PowerShell commands
This commit is contained in:
Stefan Scherer 2020-07-15 08:55:44 +02:00 committed by GitHub
commit fe2686fe00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 10 deletions

View File

@ -38,7 +38,7 @@ For now, we will create the network first and attach the MySQL container at star
``` ```
1. Start a MySQL container and attach it the network. We're also going to define a few environment variables that the 1. Start a MySQL container and attach it the network. We're also going to define a few environment variables that the
database will use to initialize the database (see the "Environment Variables" section in the [MySQL Docker Hub listing](https://hub.docker.com/_/mysql/)) (replace the ` \ ` characters with `` ` `` in Windows PowerShell). database will use to initialize the database (see the "Environment Variables" section in the [MySQL Docker Hub listing](https://hub.docker.com/_/mysql/)).
```bash ```bash
docker run -d \ docker run -d \
@ -49,6 +49,17 @@ For now, we will create the network first and attach the MySQL container at star
mysql:5.7 mysql:5.7
``` ```
If you are using PowerShell then use this command.
```powershell
docker run -d `
--network todo-app --network-alias mysql `
-v todo-mysql-data:/var/lib/mysql `
-e MYSQL_ROOT_PASSWORD=secret `
-e MYSQL_DATABASE=todos `
mysql:5.7
```
You'll also see we specified the `--network-alias` flag. We'll come back to that in just a moment. You'll also see we specified the `--network-alias` flag. We'll come back to that in just a moment.
!!! info "Pro-tip" !!! info "Pro-tip"
@ -165,11 +176,11 @@ The todo app supports the setting of a few environment variables to specify MySQ
With all of that explained, let's start our dev-ready container! With all of that explained, let's start our dev-ready container!
1. We'll specify each of the environment variables above, as well as connect the container to our app network (replace the ` \ ` characters with `` ` `` in Windows PowerShell). 1. We'll specify each of the environment variables above, as well as connect the container to our app network.
```bash hl_lines="3 4 5 6 7" ```bash hl_lines="3 4 5 6 7"
docker run -dp 3000:3000 \ docker run -dp 3000:3000 \
-w /app -v ${PWD}:/app \ -w /app -v "$(pwd):/app" \
--network todo-app \ --network todo-app \
-e MYSQL_HOST=mysql \ -e MYSQL_HOST=mysql \
-e MYSQL_USER=root \ -e MYSQL_USER=root \
@ -179,6 +190,20 @@ With all of that explained, let's start our dev-ready container!
sh -c "yarn install && yarn run dev" sh -c "yarn install && yarn run dev"
``` ```
If you are using PowerShell then use this command.
```powershell hl_lines="3 4 5 6 7"
docker run -dp 3000:3000 `
-w /app -v "$(pwd):/app" `
--network todo-app `
-e MYSQL_HOST=mysql `
-e MYSQL_USER=root `
-e MYSQL_PASSWORD=secret `
-e MYSQL_DB=todos `
node:12-alpine `
sh -c "yarn install && yarn run dev"
```
1. If we look at the logs for the container (`docker logs <container-id>`), we should see a message indicating it's 1. If we look at the logs for the container (`docker logs <container-id>`), we should see a message indicating it's
using the mysql database. using the mysql database.

View File

@ -36,18 +36,27 @@ So, let's do it!
1. Make sure you don't have any previous `getting-started` containers running. 1. Make sure you don't have any previous `getting-started` containers running.
1. Run the following command (replace the ` \ ` characters with `` ` `` in Windows PowerShell). We'll explain what's going on afterwards: 1. Run the following command. We'll explain what's going on afterwards:
```bash ```bash
docker run -dp 3000:3000 \ docker run -dp 3000:3000 \
-w /app -v ${PWD}:/app \ -w /app -v "$(pwd):/app" \
node:12-alpine \ node:12-alpine \
sh -c "yarn install && yarn run dev" sh -c "yarn install && yarn run dev"
``` ```
If you are using PowerShell then use this command.
```powershell
docker run -dp 3000:3000 `
-w /app -v "$(pwd):/app" `
node:12-alpine `
sh -c "yarn install && yarn run dev"
```
- `-dp 3000:3000` - same as before. Run in detached (background) mode and create a port mapping - `-dp 3000:3000` - same as before. Run in detached (background) mode and create a port mapping
- `-w /app` - sets the "working directory" or the current directory that the command will run from - `-w /app` - sets the "working directory" or the current directory that the command will run from
- `-v ${PWD}:/app` - bind mount the current directory from the host in the container into the `/app` directory - `-v "$(pwd):/app"` - bind mount the current directory from the host in the container into the `/app` directory
- `node:12-alpine` - the image to use. Note that this is the base image for our app from the Dockerfile - `node:12-alpine` - the image to use. Note that this is the base image for our app from the Dockerfile
- `sh -c "yarn install && yarn run dev"` - the command. We're starting a shell using `sh` (alpine doesn't have `bash`) and - `sh -c "yarn install && yarn run dev"` - the command. We're starting a shell using `sh` (alpine doesn't have `bash`) and
running `yarn install` to install _all_ dependencies and then running `yarn run dev`. If we look in the `package.json`, running `yarn install` to install _all_ dependencies and then running `yarn run dev`. If we look in the `package.json`,

View File

@ -49,11 +49,11 @@ And now, we'll start migrating a service at a time into the compose file.
## Defining the App Service ## Defining the App Service
To remember, this was the command we were using to define our app container (replace the ` \ ` characters with `` ` `` in Windows PowerShell). To remember, this was the command we were using to define our app container.
```bash ```bash
docker run -dp 3000:3000 \ docker run -dp 3000:3000 \
-w /app -v ${PWD}:/app \ -w /app -v "$(pwd):/app" \
--network todo-app \ --network todo-app \
-e MYSQL_HOST=mysql \ -e MYSQL_HOST=mysql \
-e MYSQL_USER=root \ -e MYSQL_USER=root \
@ -63,6 +63,20 @@ docker run -dp 3000:3000 \
sh -c "yarn install && yarn run dev" sh -c "yarn install && yarn run dev"
``` ```
If you are using PowerShell then use this command.
```powershell
docker run -dp 3000:3000 `
-w /app -v "$(pwd):/app" `
--network todo-app `
-e MYSQL_HOST=mysql `
-e MYSQL_USER=root `
-e MYSQL_PASSWORD=secret `
-e MYSQL_DB=todos `
node:12-alpine `
sh -c "yarn install && yarn run dev"
```
1. First, let's define the service entry and the image for the container. We can pick any name for the service. 1. First, let's define the service entry and the image for the container. We can pick any name for the service.
The name will automatically become a network alias, which will be useful when defining our MySQL service. The name will automatically become a network alias, which will be useful when defining our MySQL service.
@ -102,7 +116,7 @@ docker run -dp 3000:3000 \
- 3000:3000 - 3000:3000
``` ```
1. Next, we'll migrate both the working directory (`-w /app`) and the volume mapping (`-v ${PWD}:/app`) by using 1. Next, we'll migrate both the working directory (`-w /app`) and the volume mapping (`-v "$(pwd):/app"`) by using
the `working_dir` and `volumes` definitions. Volumes also has a [short](https://docs.docker.com/compose/compose-file/#short-syntax-3) and [long](https://docs.docker.com/compose/compose-file/#long-syntax-3) syntax. the `working_dir` and `volumes` definitions. Volumes also has a [short](https://docs.docker.com/compose/compose-file/#short-syntax-3) and [long](https://docs.docker.com/compose/compose-file/#long-syntax-3) syntax.
One advantage of Docker Compose volume definitions is we can use relative paths from the current directory. One advantage of Docker Compose volume definitions is we can use relative paths from the current directory.
@ -145,7 +159,7 @@ docker run -dp 3000:3000 \
### Defining the MySQL Service ### Defining the MySQL Service
Now, it's time to define the MySQL service. The command that we used for that container was the following (replace the ` \ ` characters with `` ` `` in Windows PowerShell): Now, it's time to define the MySQL service. The command that we used for that container was the following:
```bash ```bash
docker run -d \ docker run -d \
@ -156,6 +170,17 @@ docker run -d \
mysql:5.7 mysql:5.7
``` ```
If you are using PowerShell then use this command.
```powershell
docker run -d `
--network todo-app --network-alias mysql `
-v todo-mysql-data:/var/lib/mysql `
-e MYSQL_ROOT_PASSWORD=secret `
-e MYSQL_DATABASE=todos `
mysql:5.7
```
1. We will first define the new service and name it `mysql` so it automatically gets the network alias. We'll 1. We will first define the new service and name it `mysql` so it automatically gets the network alias. We'll
go ahead and specify the image to use as well. go ahead and specify the image to use as well.