Code Chewing Guides

PAGE 2 OF 7
  • Privately expose a service / container port with docker compose

    When using docker compose, there are multiple ways to expose / publish a service / container port so that another container can access the service. Depending on the service, you might only want the published port to remain inside the private docker network, and not exposed to the internet.

    For example, perhaps you've got a MySQL database running inside a container - you probably don't want to expose the default port (3306) to the world wide web, right? But, you do want to expose it to the other containers within the docker network. On the flip side of this, your web application running the website will rightly want to expose itself to the www.

    Setting up the public container

    Let's suppose you've already got your Dockerfile in place for your public container build… be it some kind of web application… it doesn't really matter. Your docker compose file (docker-compose.yml) would look something like this:

    version: '2'
    
    networks:
      privatenetwork:
    
    services:
      site:
        build: .
        ports:
          - "80:80"
          - "443:443"
        networks:
          - privatenetwork
    
    Read more
  • How to run a command inside a docker container

    If you've built and launched a docker image, you might be left wondering what's actually going on within the container? What does the file system look like? Does that package exist?

    Perhaps you want to check some configuration setting, or check to see if a module or particular service is enabled and running… well there's a way.

    Get the docker container id

    If you don't know the docker image name or container id, begin by listing the running docker containers:

    $ docker ps
    CONTAINER ID  IMAGE         COMMAND         CREATED         STATUS         PORTS                         NAMES
    7015544736d9  fa21f0188248  "docker-ph..."  11 seconds ago  Up 10 seconds  80/tcp, 0.0.0.0:443->443/tcp  codechewing
    
    Read more
  • Customise your modernizr build using npm

    Using the modernizr CLI, combined with some npm action, you can craft a custom modernizr bundle, serving a reduced download size for your users, by only including the specific feature detects you actually need.

    Setup modernizr bundle config

    Grab a copy of modernizr, via npm:

    npm i modernizr --save
    

    Create the following modernizr.json file:

    Read more
  • How to remove nginx version from HTTP header

    The default nginx installation includes its version number, as one of the HTTP response headers. For example, your HTTP response headers might reveal something like this:

    HTTP/1.1 200 OK
    Server: nginx/1.13.0
    Date: Thu, 18 May 2017 20:03:43 GMT
    Content-Type: text/html; charset=utf-8
    Content-Length: 7740
    Connection: keep-alive
    

    In this case, note the Server: nginx/1.13.0 header and value, exposing the version details of nginx.

    Update nginx config

    Open up the main nginx config file, probably located at /etc/nginx/nginx.conf. Locate the http section within this file, and add in the following directive:

    Read more
  • How to add gzip file compression on nginx

    Adding gzip file compression can speed up the response time for your website. As with many optimisations, it's not a silver bullet, as compressing files on the server consumes CPU effort. However, the download size of transferred files is reduced for your end users.

    This guide will show you how to setup gzip compression for the nginx server.

    Enable gzip compression

    Open up the main nginx config file, probably located at /etc/nginx/nginx.conf. Locate the http section within this file, and add in the following gzip directives & values:

    http {
        # ...
    
        gzip on;
        gzip_comp_level 4;
        gzip_types text/plain text/css application/javascript image/png;
    
        # ...
    }
    
    Read more
Code Chewing Twitter