# "Trying Out Alpine Linux" Solution Walkthrough ## Run your container Run this command to create a container and enter an interactive shell inside the container (this command is provided in the instructions—we'll learn more about how to write a command like this from scratch as we go through the week). ```shell= docker container run -it --mount source=alpine-project,target=/app alpine sh ``` ## Install packages Once you are inside your container, you can run `apk add` to download the required packages node, npm, python, nano, and curl. ```shell= apk add nodejs npm python3 nano curl ``` ## Create your Express server Next, change directories to the "app" directory, and initialize an npm project. ```shell= cd app npm init -y ``` Now we can use `npm` to install our server! ```shell= npm install express ``` And then we can write a very simple server. Let's use the text editor nano (that we installed earlier) to write a simple server. We can create and open the file with the following command: ```shell= nano express-app.js ``` Then once we are inside that file, we can put together a simple server—something like this: ```javascript= const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello from the Express app!'); }); app.listen(port, () => { console.log(`Express app listening at http://localhost:${port}`) }); ``` Save and close the file, and finally, we can run the server with ```shell= node express-app.js ``` You'll know the server is running because you'll see the message `Express app listening at http://localhost:3000` appear in the terminal. Congrats you are successfully running a server! ## If you want to check that your Express server is actually running... If you go to `http://localhost:3000`, you won't actually see the server. That's because it's only running inside the container. The information isn't exposed to your whole machine's localhost. Starting tomorrow, we'll learn about exposing ports from inside your container. In the meantime, we'll have to be inside your container to see what our server is serving. You don't need to check—your server really is running (assuming you saw that `console.log` statement when you tried to run it)—so, feel free to skip down to the next section. However, if you really want to see the contents on your server, here are the steps you could take: Open a new terminal (don't close the old one), and open another interactive terminal session with the same container. To do that, you'll need to know the name of the container, and you can find that out using the command `docker container ls`. ```shell= docker container ls # output: CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 028c14760c35 alpine "sh" 56 minutes ago Up 56 minutes quirky_carson ``` Once you know the name of your container (it's under "NAMES"—in this example it is "quirky_carson"), you can use that name to open a second interactive session with the same container. Run the following command, but replace "quirky_carson" with the name of the container that is running on your machine. ```shell= docker container exec -it quirky_carson sh ``` Once inside, you can use `curl` to see what's happening on localhost inside the container ```shell= curl localhost:3000 ``` Now you can see the message `Hello from the Express app!` in response—proof that your server is running correctly! ## Flask server First, stop the Express server with `ctrl`+`c`. Now, we need to install pip, and then use pip to install pipenv. ```shell= curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python3 get-pip.py pip install pipenv ``` Then we can use pipenv to install flask and dot-env. ```shell= pipenv install flask python-dotenv ``` Create a .flaskenv for our flask variables. ```shell= nano .flaskenv ``` Inside the .flaskenv, add ```shell= FLASK_APP=app.py FLASK_ENV=development ``` Now you can create your Flask application: ```shell= nano app.py ``` Inside your `app.py` file, add the following content: ```python= from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'Hello from the Flask app!' ``` Once we exit nano, we can run our server with the following command: ```shell= pipenv run flask run ``` You should see some familiar output in your terminal—that means your flask application is running! ## If you want to check that your Flask server is actually running... Just like before, if we want to examine what's on our container, we will need to open another terminal and create a second interactive session with the same container. Your container name should be the same. Here is the command you would use: ```shell= docker container exec -it container_name sh curl localhost:5000 ``` Now you should see the following message: ```shell Hello from the Flask app! ``` Congrats, you just ran two different types of servers inside a Docker container on your computer! ## Awesome job! In this project you installed the necessary components for bare bones Flask and Express servers on a small Alpine Linux distribution. Moving forward, you'll see how you can automate the commands that you performed in the shell by creating your own custom images based off of the alpine one you used today. You'll be able to start up containers with a single command and see your servers running and responding to requests from your browser.